<template>
  <el-drawer
    :append-to-body="true"
    size="1300px"
    title="我是标题"
    :visible="show"
    @close="handleClose"
    :with-header="false"
  >
    <div class="box">
      <AnswerPaperComponent
        :examInfo="examInfo"
        :topicTotalNum="topicTotalNum"
        :endTime="endTime"
        :loading="loading"
        enterType="drawer"
        :paperType="paperType"
        :componentType="componentType"
      >
      </AnswerPaperComponent>
    </div>
  </el-drawer>
</template>

<script>
import AnswerPaperComponent from '@/views/home/components/AnswerPaperComponent'
import { getExamPaperApi } from '@/api/ExaminationModule/exam'
import { returnName, returnType } from '@/utils/util'

export default {
  name: "app",
  components: { AnswerPaperComponent },
  data() {
    return {
      endTime:null,
      paperType:1,
      componentType:'detail',
      examInfo: {},
      topicTotalNum:0,
      loading: false,
    };
  },
  props: {
    examType: [String, Number],
    show: {
      type: Boolean,
      default: false,
    },
    id: [String, Number],
    studentId: [String, Number],
    mark: {
      required: false,
      type: String,
      default: "",
    }
  },
  watch: {
    show(val) {
      if (val) {
        this.getExaminationAnswerList();
      }
    },
  },
  mounted() {},
  methods: {
    async getExaminationAnswerList() {
      this.loading = true;
      try {
        let params = {
          studentId: this.studentId
        };
        let res = await getExamPaperApi(this.id, params);
        const {code,data} = res
        if (code==200){
          this.paperType = data.ccExamPaper.examType;
          data.ccExamPaper.taskInfo = data.taskInfo
          let formatData = this.formatDataInfo(data.ccExamPaper);
          this.examInfo = formatData.formatObj
          this.topicTotalNum = formatData.topicTotalNum
        }
      } catch (e) {
        console.error(e);
      }finally {
        this.loading = false;
      }
    },
    /**
     * 格式化考试数据
     * @param {Object} data - 原始考试数据
     * @returns {Object} 包含格式化后的数据(formatObj)和试题总数(topicTotalNum)
     */
    formatDataInfo(data) {
      // 计算试题总数
      const topicTotalNum = data.examPaperTitleList.reduce(
        (total, paperElement) => total + paperElement.topicNum, 0
      );
      // 处理每个大题
      const formatTitleList = data.examPaperTitleList.map(paperElement => {
        // 处理每个小题
        const topicList = paperElement.topicList.map(topicElement => {
          // 初始化基础对象
          const baseObj = {
            ...topicElement,
            userAnswer: "",
            multiAnswers: [],
            echoFile: [],
            settlePic: topicElement.settlePic ? topicElement.settlePic.split(",") : [],
            showDialogState: false,
            showCameraDialog: false
          };
          // 处理有答案的情况
          if (topicElement.topicAnswer) {
            // 处理选择题选项状态
            if ([0, 1].includes(topicElement.topicType)) {
              topicElement.topicOptionList.forEach(item => {
                item.isChecked = topicElement.topicAnswer.answer == item.label || (topicElement.topicAnswer.multiAnswers || []).includes(item.label);
              });
            }
            // 更新答案相关字段
            const answerObj = {
              userAnswer: topicElement.topicAnswer.answer || "",
              multiAnswers: topicElement.topicAnswer.multiAnswers || []
            };
            // 处理文件信息
            if (topicElement.topicAnswer.fileInfo) {
              answerObj.echoFile = JSON.parse(topicElement.topicAnswer.fileInfo).map(file => {
                if (file.url) {
                  return {
                    ...file,
                    type: returnType(file.url),
                    name: file.name ? file.name : returnName(file.url)
                  };
                }
                return file;
              });
            }
            Object.assign(baseObj, answerObj);
          } else if ([0, 1].includes(topicElement.topicType)) {
            // 初始化未作答的选择题选项状态
            topicElement.topicOptionList.forEach(item => {
              item.isChecked = false;
            });
          }
          return baseObj;
        });
        return {
          ...paperElement,
          topicList
        };
      });
      // 构建最终返回对象
      const formatObj = {
        ...data,
        examPaperTitleList: formatTitleList,
        finishTime: [1, -1, 3].includes(data.passing)
      };
      return { formatObj, topicTotalNum };
    },
    handleClose() {
      this.$emit("update:show", false);
    },
  },
};
</script>

<style scoped lang='scss'>
.box {
  width: 100%;
  height: 100%;
  background: #f5f6f7;
}
</style>
